<?php

namespace App\Admin\Controllers;

use App\Models\DispatchRule;
use App\Models\UserGroup;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;

class DispatchRuleController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body(Alert::make("!!!!线索进来将会逐条按 !!优先权!! 由高到低进行规则条件匹配!!!!"))
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(DispatchRule::with(['userGroup']), function (Grid $grid) {
            $grid->model()->orderBy('order');
            $grid->sortable();

            $grid->column('order','优先权');
            $grid->column('name');
            $grid->column('rules')
                ->display(function ($val) {
                    return $this->type === 0 ? "无规则全线索" : count($val) . '条规则';
                })
                ->label();
            $grid->column('userGroup.name', '分配分组');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DispatchRule(), function (Show $show) {
            $show->field('id');
            $show->field('rules');
            $show->field('name');
            $show->field('group_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DispatchRule(), function (Form $form) {
            $form->hidden('order')->default(0);
            $form->text('name')->required();

            $form->radio('type')
                ->options(DispatchRule::TYPE_OPTIONS)
                ->when(1, function (Form $form) {
                    $form->radio('rule_type')
                        ->options(DispatchRule::RULE_TYPE_OPTIONS)
                        ->default(0);

                    $form->table('rules', function (Form\NestedForm $form) {
                        $form->select('field', '字段')
                            ->options(DispatchRule::CLUE_FIELDS)
                            ->rules("required_if:type,1");
                        $form->select('operator', '条件')->options(DispatchRule::OPERATOR)
                            ->default('like')
                            ->rules("required_if:type,1");

                        $form->text('value', '值');
                    })->default([
                        [
                            'field' => '',
                            'operator' => '',
                            'value' => '',
                        ]
                    ]);
                })
                ->default(0)
                ->required();

            $options = UserGroup::query()->select(['id', 'name'])->pluck('name', 'id');
            $form->select('group_id', '接线分组')->options($options)->required();

            $form->submitted(function (Form $form) {
                if (!request()->exists(['_file_', 'upload_column', 'size'])) {
                    if ($form->input('type')) {
                        $address = collect($form->input('rules'))->filter(function ($item) {
                            return !$item['_remove_'] && $item['field'] && $item['operator'] && $item['value'];
                        });
                        if (!$address->count()) {
                            $form->responseValidationMessages('rules', '规则不能为空');
                        }
                    }
                }
            });
        });
    }
}
